package com.example.zsfinacialmanage.other;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.activity.PayDetailActivity;
import com.example.zsfinacialmanage.bean.OutpayBean;
import com.example.zsfinacialmanage.db.MyDBHelper;
public class OutManageActivity extends AppCompatActivity {
    private EditText et_money,et_time,et_payer,et_remark;
    private Spinner sp_type;
    private Button btn_modify,btn_delete;
    private MyDBHelper mhelper;
    private SQLiteDatabase db;
    private OutpayBean outpayBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out_manage);
        initView();
        getDataDisplay();
        btnModify();
        btnDelete();
    }
    private void initView(){
        et_money=findViewById(R.id.et_money_outmag);
        et_time=findViewById(R.id.et_time_outmag);
        et_payer=findViewById(R.id.et_payer_outmag);
        et_remark=findViewById(R.id.et_remark_outmag);
        sp_type=findViewById(R.id.sp_type_outmag);
        btn_modify=findViewById(R.id.bt_modify_outmag);
        btn_delete=findViewById(R.id.bt_delete_outmag);
        mhelper=new MyDBHelper(OutManageActivity.this);
        db=mhelper.getWritableDatabase();
    }
    private void getDataDisplay() {
        outpayBean = (OutpayBean) getIntent().getSerializableExtra("seri");
        et_money.setText(outpayBean.getMoney() + "");
        et_time.setText(outpayBean.getTime());
        if (outpayBean.getType().equals("电影-娱乐")) {
            sp_type.setSelection(1);
        } else if (outpayBean.getType().equals("美食-畅饮")) {
            sp_type.setSelection(2);
        } else if (outpayBean.getType().equals("欢乐-购物")) {
            sp_type.setSelection(3);
        } else if (outpayBean.getType().equals("手机-充值")) {
            sp_type.setSelection(4);
        } else if (outpayBean.getType().equals("交通-出行")) {
            sp_type.setSelection(5);
        } else if (outpayBean.getType().equals("教育-培训")) {
            sp_type.setSelection(6);
        } else if (outpayBean.getType().equals("社交-礼仪")) {
            sp_type.setSelection(7);
        } else if (outpayBean.getType().equals("生活-日用")) {
            sp_type.setSelection(8);
        }else if (outpayBean.getType().equals("其他")) {
            sp_type.setSelection(9);
        }else {
            sp_type.setSelection(0);
        }
        et_payer.setText(outpayBean.getPayee());
        et_remark.setText(outpayBean.getRemark());
    }
    private void btnModify(){
        btn_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values=new ContentValues();
                values.put("outmoney",et_money.getText().toString());
                values.put("outtime",et_time.getText().toString());
                values.put("outtype",et_payer.getText().toString());
                values.put("outpayee",et_payer.getText().toString());
                values.put("outremark",et_remark.getText().toString());
                db.update("pay_out",values,"id=?",new String[]{outpayBean.getId() + ""});
                Toast.makeText(OutManageActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(OutManageActivity.this, PayDetailActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    private void btnDelete(){
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.execSQL("delete from in_come where id=?",new String[]{outpayBean.getId()+""});
                Toast.makeText(OutManageActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(OutManageActivity.this, PayDetailActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}